<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="copyright" content="All Rights Reserved, Copyright (C) 2013, Wuyeguo, Ltd." />
<title>EasyUI Web Admin Power by Wuyeguo</title>
<link rel="stylesheet" type="text/css" href="../easyui/1.5.3/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="../css/wu.css" />
<link rel="stylesheet" type="text/css" href="../css/icon.css" />
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../js/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="../js/jquery.serializejson.min.js"></script>
<script type="text/javascript" src="../xheditor/xheditor-1.2.2.min.js"></script>
<script type="text/javascript" src="../xheditor/xheditor_lang/zh-cn.js"></script>
</head>
<body>
	         <div id="user-toolbar">
            <div class="wu-toolbar-button">
                <a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="openUserAdd()" plain="true">添加评论</a>
                <a href="#" class="easyui-linkbutton" iconCls="icon-edit" onclick="openUserEdit()" plain="true">修改评论</a>
                <a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="removeUser()" plain="true">删除评论</a>
                
                <a href="#" class="easyui-linkbutton" iconCls="icon-reload" onclick="reloadUser()" plain="true">刷新评论</a>
               
            </div>
            <div class="wu-toolbar-search">
                <label>起始时间：</label><input class="easyui-datebox" style="width:100px">
                <label>结束时间：</label><input class="easyui-datebox" style="width:100px">
                <label>用户组：</label> 
                <select class="easyui-combobox" panelHeight="auto" style="width:100px">
                    <option value="0">选择用户组</option>
                    <option value="1">黄钻</option>
                    <option value="2">红钻</option>
                    <option value="3">蓝钻</option>
                </select>
                <label>关键词：</label><input class="wu-text" style="width:100px">
                <a href="#" class="easyui-linkbutton" iconCls="icon-search">开始检索</a>
            </div>
        </div>
                <table id="users-datagrid" ></table>

<!-- 增加 -->
<div id="user-dialog" class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'" style="width:400px; padding:10px;">
    <form id="user-form" method="post">
        <table>
            <tr>
                <td width="60" align="right">评论者:</td>
                <td><input type="text" name="name" class="wu-text" /></td>
            </tr>
            <!-- <tr>
                <td align="right">点赞数:</td>
                <td><input type="text" name="thump" class="wu-text" /></td>
            </tr> -->
            <tr>
                <td align="right">评论内容:</td>
                <td><textarea class="xheditor" cols="50" rows="6" name="content"></textarea></td>
            </tr>
            <!-- <tr>
                <td valign="top" align="right">评论时间:</td>
                <td><textarea name="date" rows="6" class="gender" style="width:260px"></textarea></td>
            </tr> -->
        </table>
    </form>
</div>
<!-- 修改 -->
<div id="wu-dialog-3" class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'" style="width:400px; padding:10px;">
              <form id="wu-form2" method="post">
                  <table>
                        <tr>
                            <td width="60" align="right">评论者:</td>
                            <td><input type="text" name="name" class="wu-text" /></td>
                        </tr>
                        <!-- <tr>
                            <td align="right">点赞数:</td>
                            <td><input type="text" name="thump" class="wu-text" /></td>
                        </tr> -->
                        <tr>
                            <td align="right">评论内容:</td>
                            <td><textarea class="xheditor" cols="50" rows="6" name="content"></textarea></td>
                        </tr>
                        <!-- <tr>
                            <td valign="top" align="right">评论时间:</td>
                            <td><textarea name="date" rows="6" class="gender" style="width:260px"></textarea></td>
                        </tr> -->
                    </table>
              </form>
          </div>
 <script type="text/javascript">

   // alert(window.location.href)
 //添加
    function openUserAdd(){
        $('#user-from').form('clear');
        $('#user-dialog').dialog({
            closed: false,
                    modal:true,
                    title: "添加信息",
                    buttons: [{
                        text: '确定',
                        iconCls: 'icon-ok',
                        handler: userAddAction
                    }, {
                        text: '取消',
                        iconCls: 'icon-cancel',
                        handler: function () {
                            $('#user-dialog').dialog('close');                    
                        }
                    }]
        });
        
    }


    function  userAddAction(){
        $('#user-form').form('submit',{
            onSubmit:function(){
                console.log($('#user-form').serializeJSON())
                if($(this).form('enableValidation').form('validate')){
                    $.ajax({
                        type:"post",
                        url:'http://localhost:3000/comment/data',
                        data:$('#user-form').serializeJSON(),

                    }).then(function(){
                        console.log(res);
                    })
                    $('#user-dialog').dialog('close');
                }else{
                    alert('no')
                }
                
            }
        })
        
    }
        function GetQueryString(name)   //获取地址栏参数  name为参数名
            {
                 var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
                 var r = window.location.search.substr(1).match(reg);
                 if(r!=null)return  unescape(r[2]); return null;
            }

//            alert(GetQueryString('id'))
           
            //跳转到对应的评论
            
            
                //var nodes=GetQueryString('id'),
                

            $.ajax({
                    type:"post",
                    url:"http://localhost:3000/comment/all",
                    success:function(res){
                        for(var i in res){
                            if(GetQueryString("id")==res[i].newId){

                                // $("h2").html(res[i].title)
                                // $("p").html(res[i].content)
                                 initList(res[i].newId)
                            }
                        }           
                    }
                })




    function initList(newId){


        
            var postUrl;
            if(newId !=undefined && newId.trim().length>0){
                queryParams = {newId: newId}
            }else{
                queryParams = {};
            }
            
        
       
        $('#users-datagrid').datagrid({
                url:'http://localhost:3000/comment/list',
                pagination:true,
                fit:true,
                pageSize:4,
                pageList:[2,4,6,8,10],
                multiSort:true,
                fitColumns:true,
                queryParams: queryParams,
                toolbar:'#user-toolbar',
                rownumbers:true,

                method:'post',
                
                columns:[[
                    {field:'checkbox',checkbox:true},
                    {field:'name',title:'评论者',width:100},
                     {field:'content',title:'评论内容',width:100},
                     {field:'date',title:'评论时间',width:100},
                    {field:'operrate',title:'管理', width:80,
                        formatter: function(value,row,index){
                            if (row.user){
                                return row.user.name;
                            } else {
                                return '<a href="javascript:void(0)" style="display:inline-block;padding:0px 5px;cursor: pointer;outline: none;text-align: center; ertical-align: middle;line-height: normal;border: 1px solid #bbb;color: #444;background: #fafafa;background: linear-gradient(to bottom,#ffffff 0,#eeeeee 100%);border-radius: 5px 5px 5px 5px" onclick="deleNews(\''+ row._id+'\')">删除</a>'
                            }
                        }
                    }
                   
                ]]
            });
        
        }
        initList();
   // 打开修改的窗口
        function openUserEdit(){
         $('#wu-form2').form('clear');
        var item = $('#users-datagrid').datagrid('getSelections');
        //alert(item.productid);//return;
        
        if(item.length>1){
            alert('暂不支持批量修改');
        }else{
            var items = $('#users-datagrid').datagrid('getSelections');
            $.ajax({
                url:'http://localhost:3000/comment/all',
                type:'post',
                success:function(data){
                    if(data){
                        var arr=data;
                        for(var i=0;i<arr.length;i++){
                            if(arr[i]._id==items[0]._id){
                                var $input=$('#wu-form2 input');
                                $input[0].value=arr[i].name;
                               $('#wu-form2 textarea').val(arr[i].content);
                               
                            }
                        }
                        
                    }
                    else{
                        //绑定值
                        $('#wu-form2').form('load', data)
                    }
                }   
            });
            $('#wu-dialog-3').dialog({
                closed: false,
                modal:true,
                title: "修改信息",
                buttons: [{
                    text: '确定',
                    iconCls: 'icon-ok',
                    handler:  edit
                }, {
                    text: '取消',
                    iconCls: 'icon-cancel',
                    handler: function () {
                        $('#wu-dialog-3').dialog('close');                    
                    }
                }]
            });
        }
        
    }   
    //修改
    function edit(){
        $('#wu-form2').form('submit', {
            onSubmit:function(){
                var items = $('#users-datagrid').datagrid('getSelections');
                if($(this).form('enableValidation').form('validate')){
                    $.ajax({
                        type:"post",
                        url:"http://localhost:3000/comment/data/"+items[0]._id,
                        data:$('#wu-form2').serializeJSON()
                    });
                    $('#wu-dialog-3').dialog('close');
                    $('#users-datagrid').datagrid('reload');
                }else{
                    alert('fail');
                }
            }
        });
    }
    //删除记录
    
    function removeUser(){
        $.messager.confirm('信息提示','确定要删除该记录？', function(result){
            if(result){
               
                 var items = $('#users-datagrid').datagrid('getSelections');
                var ids = [];
                $(items).each(function(){
                    ids.push(this.productid);   
                });
                //alert(ids);return;
                $.ajax({
                   url:"http://localhost:3000/comment/data/"+ items[0]._id,
                    
                   type:"delete",
                    success:function(ids){
                        if(ids){
                            $.messager.alert('信息提示','删除成功！','info'); 
                              $('#users-datagrid').datagrid('reload');  
                        }
                        else
                        {
                            $.messager.alert('信息提示','删除失败！','info');        
                        }
                    }   
                });
            }   
        });
    }
    
    //刷新
    function reloadUser(){
        $('#users-datagrid').datagrid('reload');
    }

    //点击管理里面的删除
    function deleNews(id){
            $.ajax({
                type:"delete",
                url:"http://localhost:3000/comment/data/"+ id
            }).then(function(res){
                $('#users-datagrid').datagrid('reload');  
            });
        }

 </script>
    </body>
</html>